import { handleActions } from 'redux-actions'
import { combineReducers } from 'redux'
import allActions from './actions/all'
import appReducer from './appReducer'
// import createGameConfig from './createGameConfig'
import confProcess from './confProcess'

global.allActions = allActions

const mallReducer = handleActions({
  'saveMallInfo'(state, action) {
    console.log('saveMallInfo!', action);
    return { ...state, ...action.payload }
  },
}, {diamonds: []})


// const config = {
//   gameList: [
//     { name: '跑得快', icon: 'img/gameIcon/pdk-icon.png', gameId: 8, createMemory: 5184 },
//     { name: '红中麻将', icon: 'img/gameIcon/majiang.png', gameId: 3, createMemory: 5179 },
//     { name: '长沙麻将', icon: 'img/gameIcon/cs-majiang.png', gameId: 10, createMemory: 5189, dev: false },
//     { name: '平江扎鸟', icon: 'img/gameIcon/pingjiang-majiang.png', gameId: 11, createMemory: 5184},
//     { name: '欢乐三张', icon: 'img/gameIcon/wansanzhang.png', gameId: 4, createMemory: 5190 },
//     { name: '经典斗牛', icon: 'img/gameIcon/classic-bull.png', gameId: 1, createMemory: 5186},
//     { name: '斗公牛', icon: 'img/gameIcon/fight-bull.png', gameId: 2, createMemory: 5183 },
//     { name: '点子牛', icon: 'img/gameIcon/dianzi-bull.png', gameId: 5, createMemory: 5189 },
//     { name: '三公', icon: 'img/gameIcon/sangong.png', gameId: 6, createMemory: 5182 },
//     { name: '平江开船', icon: 'img/gameIcon/sangong.png', gameId: 6, createMemory: 5178, dev: true }
//   ]
// }

// if (env !== 'dev') {
//   config.gameList = config.gameList.filter((gameData: any) => gameData.dev !== true)
// }
// config.gameList.forEach((gameData: any) => {
  // createGameConfig.forEach(conf => {
  //   if (conf.name === gameData.name) {
  //     const cName = conf.name + gameData.createMemory
  //     if (localStorage.getItem(cName)) {
  //       conf.options = JSON.parse(localStorage.getItem(cName))
  //     }
  //     gameData.config = conf.options
  //     gameData.payTypeInfo = conf.payTypeInfo
  //   }
  // })
// })

// const configReducer = handleActions({
//   'saveMetas'(state, action) {
//     return { ...state, ...action.payload }
//   },
// }, config)

const dynamics = {
  showCreateGame: false,
  currentGame: {},
  showModal: false,
  modalContent: '',
  showGroupModal: false,
  groupModalContent: '',
  shareCrowdIdValid: true
}
const dynamicReducer = handleActions({
  'openCreateGame'(state, action) {
    return { ...state, currentGame: action.payload, showCreateGame: true }
  },
  'closeCreateGame'(state) {
    return { ...state, ...dynamics }
  },
  'shareCrowdIdInvalid'(state) {
    return { ...state, shareCrowdIdValid: false }
  },
  'confClicked'(state, action){
    const {index, value} = action.payload
    const cgCopy = confProcess.configClicked(state.currentGame, index, value)
    return { ...state, currentGame: cgCopy }
  },
  'openModal'(state, action) {
    return { ...state, showModal: true, modalContent: action.payload }
  },
  'closeModal'(state) {
    return { ...state, showModal: false }
  },
  'openGroupModal'(state, action) {
    return { ...state, showGroupModal: true, groupModalContent: action.payload }
  },
  'closeGroupModal'(state) {
    return { ...state, showGroupModal: false }
  },
}, dynamics)

const group = {
  chooseCreateGame: undefined,
  currentGroup: undefined,
  currentGroupRules: [],
  currentSelectedGroupRule: {},
  currentTables: [],
  loadComplete: false
}
const groupReducer = handleActions({
  'chooseCreateGame'(state, action) {
    return { ...state, chooseCreateGame: action.payload }
  },
  'ruleConfClicked'(state, action){
    const {index, value} = action.payload
    const cgCopy = confProcess.configClicked(state.chooseCreateGame, index, value)
    return { ...state, chooseCreateGame: cgCopy }
  },
  'changeCurrentGroup'(state, action) {
    localStorage.setItem('currentGroup', JSON.stringify(action.payload))
    return { ...state, currentGroup: action.payload }
  },
  'currentGroupRules'(state, action) {
    return { ...state, currentGroupRules: action.payload, loadComplete: true }
  },
  'currentSelectedGroupRule'(state, action) {
    return { ...state, currentSelectedGroupRule: action.payload }
  },
  'currentTableData'(state, action) {
    return { ...state, currentTables: action.payload }
  }
}, group)

const rootReducer = combineReducers({
  app: appReducer,
  mall: mallReducer,
  // config: configReducer,
  dynamics: dynamicReducer,
  group: groupReducer
})

export default rootReducer
